﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Aliyun.MQS.SDK
{
    public class AliyunMQSClient
    {
        private readonly string _requestUriStirng;
        private readonly string _accessKeySecret;
        private readonly string _accessKeyID;

        public AliyunMQSClient(string requestUriString, string accessKeyID, string accessKeySecret)
        {
            this._requestUriStirng = requestUriString;
            this._accessKeyID = accessKeyID;
            this._accessKeySecret = accessKeySecret;
        }

        public Task<bool> CreateQueueAsync(CreatingQueueOption option)
        {
            var task = Task.Run<bool>(() =>
            {
                var encoded = PluginManager.Instance.Resolve<IEncodingPlugin>();

                var client = this.CreateRequest();
                client.Method = "put";

                //Set the common header.
                PluginManager.Instance.Resolve<ICommonHeaderPlugin>().SetCommonHeader(client, this._accessKeyID, this._accessKeySecret);

                //Set the body of request.
                using (var sw = new StreamWriter(client.GetRequestStream(), encoded.Encoded))
                {
                    option.WriteToStreamAsXml(sw);
                }

                //Read the respose.
                using (var res = client.GetResponse())
                {
                    return true;
                }
            });
            return task;
        }

        public Task<SendMessageOptionResponse> SendMessageAsync(SendMessageOption option)
        {
            var task = Task.Run<SendMessageOptionResponse>(() =>
                {
                    var encoded = PluginManager.Instance.Resolve<IEncodingPlugin>();

                    var client = this.CreateRequest(string.Format("{0}/messages", this._requestUriStirng));
                    client.Method = "post";

                    //Set the common header.
                    PluginManager.Instance.Resolve<ICommonHeaderPlugin>().SetCommonHeader(client, this._accessKeyID, this._accessKeySecret);

                    //Set the body of request.
                    using (var sw = new StreamWriter(client.GetRequestStream(), encoded.Encoded))
                    {
                        option.WriteToStreamAsXml(sw);
                    }

                    //Read the respose.
                    using (var res = client.GetResponse())
                    {
                        var result = res.GetResponseStream();
                        var xDoc = new System.Xml.XmlDocument();
                        var nm = new System.Xml.XmlNamespaceManager(xDoc.NameTable);
                        nm.AddNamespace("mqs", "http://mqs.aliyuncs.com/doc/v1/");
                        xDoc.Load(result);
                        

                        var f = xDoc.SelectSingleNode("/mqs:Message/mqs:MessageId", nm);
                        return new SendMessageOptionResponse("", "");
                    }
                });
            return task;
        }

        protected HttpWebRequest CreateRequest()
        {
            return CreateRequest(this._requestUriStirng);
        }
        protected HttpWebRequest CreateRequest(string uri)
        {
            var client = WebRequest.Create(uri) as HttpWebRequest;
            return client;
        }
    }
}
